package com.wally.controller;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wally.entity.QueueMsg;
import com.wally.service.Producer;
import com.wally.util.ResponseUtil;

/**
 * 消息接口
 *
 * @author laigl 2017年6月27日
 *
 */
@RestController
@RequestMapping("/mq")
public class ActiveMQController {

	public static final Logger log = LoggerFactory.getLogger(ActiveMQController.class);

	private static final String QUEUE_NAME = "integral";

	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	@Autowired
	private Producer producer;

	@GetMapping(value = "/send2")
	public Map<String, Object> send2() {

		producer.sendMessage();

		return ResponseUtil.trueBizResult("消息入队成功!!!");
	}

	@GetMapping(value = "/send")
	public Map<String, Object> sendMessage() {

		QueueMsg msg = new QueueMsg(UUID.randomUUID().toString().replace("-", ""), LocalDateTime.now().toString());

		jmsMessagingTemplate.convertAndSend(QUEUE_NAME, msg);

		return ResponseUtil.trueBizResult("消息入队成功!!!");
	}

	@GetMapping(value = "/get")
	public Map<String, Object> receiveMethod() {

		QueueMsg message = jmsMessagingTemplate.receiveAndConvert(QUEUE_NAME, QueueMsg.class);

		return ResponseUtil.trueBizResult(message);
	}

}
